package com.zxshhy.listener;

import com.zxshhy.zookeeper.ZKClientBase;
import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class ZKNodeListener implements CommandLineRunner {
    @Resource
    private ZKClientBase zkClientBase;

    @Override
    public void run(String... args) throws Exception {
        log.info("服务启动,初始化zookeeper节点监听");
        ZkClient zkClient = zkClientBase.init();
        zkClient.subscribeChildChanges("/zxshhy", new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
                log.info("监听到handleChildChange:{}={}",s,list);
            }
        });
        zkClient.subscribeDataChanges("/zxshhy", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                log.info("监听到handleDataChange:{}={}",s,o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                log.info("监听到handleDataDeleted:{}",s);
            }
        });
    }
}
